<?php
class IjformHelper extends AppHelper {
  public $helpers = array('Form', 'Html');
  public $model;
  public $fields;

  public $ij;

  // set the infojabotabek
  public function setIj($ij) {
    $this->ij = $ij;
  }

  public function renderForm($model, $fields, $options = array()) {
    $this->model = $model;
    $this->fields = $fields;

    $form = $this->Form->create($model, $options);
    foreach ($fields as $field => $rule) {
      if ( !isset($rule['type']) ) {
        $rule['type'] = 'text';
      }
      $inputRenderer = 'render' . ucfirst($rule['type']);
      if ( method_exists($this, $inputRenderer) ) {
        $input = $this->{$inputRenderer}($field);
      } else {
        // default input renderer
        $_options = array(
          'div' => false,
          'class' => 'long'
        );
        if ( isset($rule['label']) ) {
          $_options['label'] = $rule['label'];
        }
        if ( isset($rule['type']) ) {
          $_options['type'] = $rule['type'];
        }
        $input = $this->Form->input($field, $_options);
      }

      // since help text can be modified after
      // calling the rendered, it should use $this->fields
      // instead of $rule.
      if ( isset($this->fields[$field]['help_text']) ) {
        $input .= $this->renderHelptext($this->fields[$field]['help_text']);
      }

      // if div option is set
      // render the input field with div
      if ( $rule['div'] ) {
        $form .= $this->renderDiv($input, $rule['div']);
      } else {
        $form .= $input;
      }
    }
    $form .= $this->Form->submit('Simpan');

    return $form;
  }

  public function renderText($field) {
    $options = array(
      'div'   => false,
      'label' => $this->fields[$field]['label'],
      'class' => $field,
      'type' => 'text',
    );
    if ( isset($this->fields[$field]['name']) ) {
      $options['name'] = $this->fields[$field]['name'];
    }
    return $this->Form->input($field, $options);
  }

  public function renderTextarea($field) {
    return $this->Form->input($field, array(
      'div'   => false,
      'type'  => 'textarea',
      'label' => $this->fields[$field]['label'],
      'class' => $field
    ));
  }

  public function renderFile($field) {
    if ( isset($this->data[$this->model][$field]) &&
         !empty($this->data[$this->model][$field]) )
    {
        $img  = sprintf('<label>%s</label>', $this->fields[$field]['label']);
        $img .= '<img src="'. $this->ij['images_url_relative_path'] .
                    $this->ij['images_resized_prefix'] .
                    $this->data[$this->model][$field] .
               '"><br />';
        $hidden = $this->Form->input($field, array(
          'type'  => 'hidden',
          'value' => $this->data[$this->model][$field]
        ));
        $img .= $hidden;

        return $img;
    }

    $options = array(
      'div'   => false,
      'type'  => 'file',
      'label' => $this->fields[$field]['label'],
      'class' => $field
    );

    if ( isset($this->fields[$field]['name']) ) {
      $options['name'] = $this->fields[$field]['name'];
    }

    return $this->Form->input($field, $options);
  }

  public function renderMultiFile($field) {
    $input  = "<div id=\"multi_{$field}\">";
    $input .= $this->renderFile($field);
    $input .= '</div>';
    $input .= '<a href="#" id="add_' . $field . '" class="add_multi">'.
              'Tambah</a>';

    $input .= '<script type="text/javascript">';
    $input .= '$(function() {';
    $input .= "$('#add_{$field}').click(function(e) {";
    $input .= "var cloned = $('.{$field}:last', '#multi_{$field}').clone();";
    $input .= "cloned.val('');";
    $input .= "$('#multi_{$field}').append('<br />');";
    $input .= "$('#multi_{$field}').append(cloned);";

    $input .= "e.preventDefault();return false;";
    $input .= '});';
    $input .= '});';
    $input .= '</script>';
    return $input;
  }

  public function renderMultitext($field) {
    $input  = "<div id=\"multi_{$field}\">";
    $input .= $this->renderText($field);
    $input .= '</div>';
    $input .= '<a href="#" id="add_' . $field . '" class="add_multi">'.
              'Tambah</a>';

    $input .= '<script type="text/javascript">';
    $input .= '$(function() {';
    $input .= "$('#add_{$field}').click(function(e) {";
    $input .= "var cloned = $('.{$field}:last', '#multi_{$field}').clone();";
    $input .= "cloned.val('');";
    $input .= "$('#multi_{$field}').append('<br />');";
    $input .= "$('#multi_{$field}').append(cloned);";

    $input .= "e.preventDefault();return false;";
    $input .= '});';
    $input .= '});';
    $input .= '</script>';
    return $input;
  }

  public function renderSelect($field) {
    $select  = sprintf('<label>%s</label>', $this->fields[$field]['label']);
    $select .= $this->Form->select($field, $this->fields[$field]['data']);
    $select .= $this->Form->isFieldError($field) ?
        $this->Form->error($field) : '';

    return $select;
  }

  public function renderMap($field) {
    if ( isset($this->fields[$field]['size']) ) {
      $size = $this->fields[$field]['size'];
    } else {
      $size = 'small';
    }

    $map  = sprintf('<label>%s</label>', $this->fields[$field]['label']);
    $map .= '<div id="map-container" class="' . $size . '-map"></div>';

    return $map;
  }

  public function renderHelptext($content) {
    return sprintf('<span class="help-text">%s</span>', $content);
  }

  public function renderDiv($content = '', $id = '') {
    return sprintf('<div %sclass="input">%s</div>',
        $id ? 'id="' . $id . '"' : '', $content);
  }
}
